package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.graduate.domain.entity.MsCommunityPost;
import com.ruoyi.graduate.domain.param.MsCommunityPostAddParam;
import com.ruoyi.graduate.domain.param.MsCommunityPostAuditParam;
import com.ruoyi.graduate.domain.param.MsCommunityPostPageParam;
import com.ruoyi.graduate.domain.param.PageBaseParam;
import com.ruoyi.graduate.domain.vo.MsCommunityPostDetailVo;
import com.ruoyi.graduate.domain.vo.MsCommunityPostPageVo;
import com.ruoyi.graduate.domain.vo.MsCommunityPostUserPageVo;
import com.ruoyi.graduate.mapper.MsCommunityPostMapper;
import com.ruoyi.graduate.mapping.MsCommunityPostMapping;
import com.ruoyi.graduate.service.IMsCommunityPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class IMsCommunityPostServiceImpl extends ServiceImpl<MsCommunityPostMapper, MsCommunityPost>
implements IMsCommunityPostService {

    @Autowired
    private MsCommunityPostMapper msCommunityPostMapper;


    @Override
    public PageInfo pageList(MsCommunityPostPageParam param) {

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsCommunityPostPageVo> msCommunityPostPageVos = msCommunityPostMapper.pageList(param.getTitle());


        return new PageInfo(msCommunityPostPageVos);
    }

    @Override
    public MsCommunityPostDetailVo detail(Integer id) {
        MsCommunityPostDetailVo detail = msCommunityPostMapper.detail(id);

        return detail;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult audit(MsCommunityPostAuditParam param) {
        UpdateWrapper uw = new UpdateWrapper();
        uw.eq("post_id", param.getPostId());
        uw.set("post_status", param.getPostStatus());
        uw.set("audit_user_id", param.getAuditUserId());
        uw.set("audit_time", LocalDateTime.now());

        int update = msCommunityPostMapper.update(null, uw);

        return update > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public PageInfo userPageList(PageBaseParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());


        List<MsCommunityPostUserPageVo> msCommunityPostUserPageVos = msCommunityPostMapper.userList();

        return new PageInfo(msCommunityPostUserPageVos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(MsCommunityPostAddParam param) {

        MsCommunityPost add = MsCommunityPostMapping.INSTANCE.add(param);

        int insert = msCommunityPostMapper.insert(add);


        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }
}
